
// 页面白名单
const whiteList = [
	'/',
	'/pages/login/login',
	'/pages/login/signup',
	'/pages/version1/index/index',
	'/pages/version1/index/agent/agent',
	'/pages/version1/index/agent/agent_sign',
	'/pages/version5/index/index',
	'/pages/version5/index/agent/agent',
	'/pages/version5/index/agent/agent_sign',
	'/pages/public/customerbind',
]

function hasPermission (url) {
	let access_token = uni.getStorageSync('userinfo')
	let access_tokenB = uni.getStorageSync('merinfo')
	let access_tokenC = uni.getStorageSync('customerinfo')
	// 在白名单中或有token，直接跳转
	if(whiteList.indexOf(url) !== -1 || access_token.token || access_tokenB.token || access_tokenC.token) {
		return true
	}
	return false
}

uni.addInterceptor('navigateTo', {
	// 页面跳转前进行拦截, invoke根据返回值进行判断是否继续执行跳转
	invoke (e) {
		let userType = uni.getStorageSync('userType')
		if(!hasPermission(e.url)){
			let equipment_number = uni.getStorageSync('equipment_number')
			if (userType == 1) {
				uni.reLaunch({
					url: '/pages/register/index?enumber=' + equipment_number
				})
			} else if (userType == 2) {
				uni.reLaunch({
					url: '/pages/merchant/index'
				})
			} else {
				uni.reLaunch({
					url: '/pages/customer/index'
				})
			}
			return false
		}
		return true
	},
	success (e) {
		// console.log(e)
	}
})

uni.addInterceptor('switchTab', {
	// tabbar页面跳转前进行拦截
	invoke (e) {
		if(!hasPermission(e.url)){
			let equipment_number = uni.getStorageSync('equipment_number')
			uni.reLaunch({
				url: '/pages/register/index?enumber=' + equipment_number
			})
			return false
		}
		return true
	},
	success (e) {
		// console.log(e)
	}
})